package moe.config;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class Configuration extends PropertiesUtil {

	private static final String[] privacyFields =
			new String[] { "db_type", "db_path", "db_host", "db_name", "db_user", "db_password",
					"http_user", "http_password", "collect_dir", "collection_library" };
	private static List<String> privacyFieldList;

	public static boolean isPrivacy(String field) {
		if (privacyFieldList == null) {
			privacyFieldList = Arrays.asList(privacyFields);
		}
		if (field.startsWith("moe.imouto")) {
			return true;
		}
		return privacyFieldList.contains(field);
	}

	/*
	 * DB config
	 */

	public static String getDriverClass() {
		String driverClass = "org.sqlite.JDBC", dbType = getProperty(Constants.DB_TYPE, "sqlite");
		if (dbType.equalsIgnoreCase("mysql")) {
			driverClass = "com.mysql.jdbc.Driver";
		}
		return driverClass;
	}

	public static String getJdbcUrl() {
		String jdbcUrl = null, dbType = getProperty(Constants.DB_TYPE, "sqlite");
		if (dbType.equalsIgnoreCase("mysql")) {
			String host = getProperty(Constants.DB_HOST);
			if (host == null) {
				throw new RuntimeException("MySQL host address can not for empty");
			}
			String name = getProperty(Constants.DB_NAME);
			if (name == null) {
				throw new RuntimeException("MySQL database name can not for empty");
			}
			if (!host.contains(":")) {
				host += ":3306";
			}
			StringBuilder builder = new StringBuilder();
			builder.append("jdbc:mysql://")
					.append(host)
					.append("/")
					.append(name)
					.append("?useUnicode=true&characterEncoding=UTF-8");
			jdbcUrl = builder.toString();
		} else {
			String path = getProperty(Constants.DB_PATH, "./moe.s3db");
			jdbcUrl = "jdbc:sqlite:" + path;
		}
		return jdbcUrl;
	}

	public static String getDbUser() {
		return getProperty(Constants.DB_USER, "");
	}

	public static String getDbPassword() {
		return getProperty(Constants.DB_PASSWORD, "");
	}

	/*
	 * common config
	 */

	public static int getPort() {
		return getInt(Constants.PORT, 8080);
	}

	public static String getHttpUser() {
		return getProperty(Constants.HTTP_USER, "");
	}

	public static String getHttpPassword() {
		return getProperty(Constants.HTTP_PASSWORD, "");
	}

	public static boolean isOpenBrowser() {
		return getBoolean(Constants.OPEN_BROWSER, false);
	}

	public static String getCollectDir() {
		String defaultCollectDir = System.getProperty("user.dir") + File.separator + "collect";
		return getProperty(Constants.COLLECT_DIR, defaultCollectDir);
	}

	public static boolean isImoutoCollectPattern() {
		String pattern = getProperty(Constants.COLLECT_PATTERN, "");
		return pattern.equalsIgnoreCase("imouto");
	}

	/*
	 * UI config
	 */

	public static int getPageSize() {
		return getInt(Constants.PAGE_SIZE, 16);
	}

	public static int getTipsNumber() {
		return getInt(Constants.TIPS_NUMBER, 5);
	}

	/*
	 * server cache config
	 */

	public static int getServerCache() {
		return getInt(Constants.SERVER_CACHE, 0);
	}

	public static int getSessionExpires() {
		return getInt(Constants.SESSION_EXPIRES, 10);
	}

	/*
	 * collection library config
	 */

	public static String getCollectionLibraryPath() {
		return getProperty(Constants.COLLECTION_LIBRARY, "");
	}

	public static boolean isCreateThumbnails() {
		return PropertiesUtil.getBoolean(Constants.COLLECTION_CREATE_THUMBNAILS, true);
	}

	public static int getLibraryRefreshInterval() {
		return getInt(Constants.COLLECTION_REFRESH_INTERVAL, 5);
	}

	/*
	 * data path config
	 */

	public static String getDefaultPath() {
		String defaultPath = getProperty(Constants.DEFAULT_PATH, "");
		if (defaultPath == null || defaultPath.trim().equals("")) {
			defaultPath = System.getProperty("user.dir");
		}
		return defaultPath;
	}
}
